package com.flca.frw.zk.util;

public class PhoneHelper 
{
	public PhoneData parse(String source) 
	{
		PhoneData result = new PhoneData();
		
		if (source != null) {
			result.setAreaCode(getAreaCode(source.trim()));
			result.setNumber(getNumber(source.trim()));
		}
		
		return result;
	}
	
	private String getAreaCode(String source) 
	{
		int delimAt = getDelim(source);
		
		if (delimAt > 0) {
			return source.substring(0, delimAt);
		} else {
			if (source.length() > 7) {
				return source.substring(0, 3); //TODO lijst met 3-cijferige telnrs ?
			}
			return null;
		}
	}

	private String getNumber(String source) 
	{
		int delimAt = getDelim(source);
	
		if (delimAt > 0) {
			return source.substring(delimAt + 1);
		} else {
			if (source.length() > 3) {
				return source.substring(3); //TODO lijst met 3-cijferige telnrs ?
			}
		}
		return null;
	}
	
	private int getDelim(String source) 
	{
		int r = 0;
		if (source.indexOf("-") > 0) {
			r = source.indexOf("-");
		} else if (source.indexOf(" ") > 0) {
			r = source.indexOf(" ");
		}
		return r;
	}

	public class PhoneData
	{
		
		private String areaCode;
		private String number;
		
		public String getAreaCode() {
			return areaCode;
		}
		public void setAreaCode(String areaCode) {
			this.areaCode = areaCode;
		}
		public String getNumber() {
			return number;
		}
		public void setNumber(String number) {
			this.number = number;
		}
		
		
	}
}
